package com.atguigu.zy.interview.juc.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
class MyResource {
    /**
     * 默认开启，进行生产+消费
     * 需要加volatile,需要及时通知
     */
    private volatile boolean FLAG = true;
    private AtomicInteger atomicInteger = new AtomicInteger();
    BlockingQueue<String> blockingQueue = null;

    public MyResource(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
        //查看具体传入的是哪一个实现类
        System.out.println(blockingQueue.getClass().getName());
    }

    /**
     *
     * 1、生产队列
     */
    public void myProd() throws Exception {
        String data = null;
        boolean retValue;
        while (FLAG) {
            /**
             * 左边需要定义在外面，复用，降低循环的资源
             */
            data = atomicInteger.incrementAndGet() + "";
            retValue = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
            if (retValue) {
                System.out.println(Thread.currentThread().getName() + "\t 插入队列生产蛋糕" + data + "成功");
            } else {
                System.out.println(Thread.currentThread().getName() + "\t 插入队列生产蛋糕" + data + "失败");
            }
            TimeUnit.SECONDS.sleep(1);
        }
        System.out.println(Thread.currentThread().getName() + "\t 大老板叫停了，表示FLAG=false,生产动作结束");
    }

    /**
     * 2、消费队列
     */
    public void myConsumer() throws Exception {
        String result = null;
        while (FLAG) {
            result = blockingQueue.poll(2L, TimeUnit.SECONDS);
            if (result == null || result.equalsIgnoreCase("")) {
                //  没取到
                FLAG = false;
                System.out.println(Thread.currentThread().getName() + "\t 超过2秒钟没有取到蛋糕，消费退出");
                System.out.println();
                System.out.println();
                System.out.println();
                return;
            }
            System.out.println(Thread.currentThread().getName() + "\t 消费队列消费蛋糕" + result + "成功");
        }
    }

    /**
     * 3、叫停
     */
    public void stop() throws Exception {
        FLAG = false;
    }


}

/**
 * @formatter:off
 * @Author : Jungle
 * @Description :
 *   volatile/CAS/atomicInteger/BlockQueue/线程交互/原子引用
 * @formatter:on
 */
public class ProdConsumerBlockQueueDemo {
    public static void main(String[] args) throws Exception {
        MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t 生产线程启动");
            try {
                myResource.myProd();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "prod").start();


        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t 消费线程启动");
            System.out.println();
            System.out.println();
            System.out.println();
            try {
                myResource.myConsumer();


            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "Consumer").start();


        //@formatter:off  线程暂停 5 秒
       try { TimeUnit.SECONDS.sleep( 5 ); } catch (InterruptedException e) { e.printStackTrace();}
       //@formatter:on
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("5秒钟时间到，大老板，main线程叫停，活动结束");
        myResource.stop();
    }
}
